home *** CD-ROM | disk | FTP | other *** search
/ CD BIT 75 / CD BIT 75.iso / Software / mysql-4.0.22-win / data1.cab / Development / examples / tests / drop_test.pl < prev    next >
Encoding:
Perl Script  |  2004-10-28  |  6.1 KB  |  221 lines

  1. #!/usr/bin/perl -w
  2. #
  3. # This is a test with uses processes to insert, select and drop tables.
  4. #
  5.  
  6. $opt_loop_count=100000; # Change this to make test harder/easier
  7.  
  8. ##################### Standard benchmark inits ##############################
  9.  
  10. use DBI;
  11. use Getopt::Long;
  12. use Benchmark;
  13.  
  14. package main;
  15.  
  16. $opt_skip_create=$opt_skip_in=$opt_verbose=$opt_fast_insert=
  17.   $opt_lock_tables=$opt_debug=$opt_skip_delete=$opt_fast=$opt_force=0;
  18. $opt_host=""; $opt_db="test";
  19.  
  20. GetOptions("host=s","db=s","loop-count=i","skip-create","skip-in","skip-delete",
  21. "verbose","fast-insert","lock-tables","debug","fast","force") || die "Aborted";
  22. $opt_verbose=$opt_debug=$opt_lock_tables=$opt_fast_insert=$opt_fast=$opt_skip_in=$opt_force=undef;  # Ignore warnings from these
  23.  
  24. print "Testing 5 multiple connections to a server with 1 insert, 2 drop/rename\n";
  25. print "1 select and 1 flush thread\n";
  26.  
  27. $firsttable  = "bench_f1";
  28.  
  29. ####
  30. ####  Start timeing and start test
  31. ####
  32.  
  33. $start_time=new Benchmark;
  34. if (!$opt_skip_create)
  35. {
  36.   $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
  37.               $opt_user, $opt_password,
  38.             { PrintError => 0}) || die $DBI::errstr;
  39.   $dbh->do("drop table if exists $firsttable, ${firsttable}_1, ${firsttable}_2");
  40.  
  41.   print "Creating table $firsttable in database $opt_db\n";
  42.   $dbh->do("create table $firsttable (id int(6) not null, info varchar(32), marker char(1), primary key(id))") || die $DBI::errstr;
  43.   $dbh->disconnect; $dbh=0;    # Close handler
  44. }
  45. $|= 1;                # Autoflush
  46.  
  47. ####
  48. #### Start the tests
  49. ####
  50.  
  51. test_insert() if (($pid=fork()) == 0); $work{$pid}="insert";
  52. test_drop(1) if (($pid=fork()) == 0); $work{$pid}="drop 1";
  53. test_drop(2) if (($pid=fork()) == 0); $work{$pid}="drop 2";
  54. test_select() if (($pid=fork()) == 0); $work{$pid}="select";
  55. test_flush() if (($pid=fork()) == 0); $work{$pid}="flush";
  56.  
  57. $errors=0;
  58. while (($pid=wait()) != -1)
  59. {
  60.   $ret=$?/256;
  61.   print "thread '" . $work{$pid} . "' finished with exit code $ret\n";
  62.   $errors++ if ($ret != 0);
  63. }
  64.  
  65. if (!$opt_skip_delete && !$errors)
  66. {
  67.   $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
  68.               $opt_user, $opt_password,
  69.             { PrintError => 0}) || die $DBI::errstr;
  70.   $dbh->do("drop table $firsttable");
  71.   $dbh->disconnect; $dbh=0;    # Close handler
  72. }
  73. print ($errors ? "Test failed\n" :"Test ok\n");
  74.  
  75. $end_time=new Benchmark;
  76. print "Total time: " .
  77.   timestr(timediff($end_time, $start_time),"noc") . "\n";
  78.  
  79. exit(0);
  80.  
  81. #
  82. # Insert records in the table
  83. #
  84.  
  85. sub test_insert
  86. {
  87.   my ($dbh,$i);
  88.  
  89.   $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
  90.               $opt_user, $opt_password,
  91.             { PrintError => 0}) || die $DBI::errstr;
  92.   for ($i=0 ; $i < $opt_loop_count; $i++)
  93.   {
  94.     if (!$dbh->do("insert into $firsttable values ($i,'This is entry $i','')"))
  95.     {
  96.       print "Warning; Got error on insert: " . $dbh->errstr . "\n" if (! ($dbh->errstr =~ /doesn't exist/));
  97.     }
  98.   }
  99.   $dbh->disconnect; $dbh=0;
  100.   print "Test_insert: Inserted $i rows\n";
  101.   exit(0);
  102. }
  103.  
  104.  
  105. sub test_drop
  106. {
  107.   my ($id) = @_;
  108.   my ($dbh,$i,$sth,$error_counter,$sleep_time);
  109.  
  110.   $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
  111.               $opt_user, $opt_password,
  112.             { PrintError => 0}) || die $DBI::errstr;
  113.   $error_counter=0;
  114.   $sleep_time=2;
  115.   for ($i=0 ; $i < $opt_loop_count ; $i++)
  116.   {
  117.     sleep($sleep_time);
  118.     # Check if insert thread is ready
  119.     $sth=$dbh->prepare("select count(*) from $firsttable") || die "Got error on select from $firsttable: $dbh->errstr\n";
  120.     if (!$sth->execute || !(@row = $sth->fetchrow_array()) ||
  121.     !$row[0])
  122.     {
  123.       $sth->finish;
  124.       $sleep_time=1;
  125.       last if ($error_counter++ == 5);
  126.       next;
  127.     }
  128.     $sleep_time=2;
  129.     $sth->finish;
  130.  
  131.     # Change to use a new table
  132.     $dbh->do("create table ${firsttable}_$id (id int(6) not null, info varchar(32), marker char(1), primary key(id))") || die $DBI::errstr;
  133.     $dbh->do("drop table if exists $firsttable") || die "Got error on drop table: $dbh->errstr\n";
  134.     if (!$dbh->do("alter table ${firsttable}_$id rename to $firsttable"))
  135.     {
  136.       print "Warning; Got error from alter table: " . $dbh->errstr . "\n" if (! ($dbh->errstr =~ /already exist/));
  137.       $dbh->do("drop table if exists ${firsttable}_$id") || die "Got error on drop table: $dbh->errstr\n";
  138.     }
  139.   }
  140.   $dbh->do("drop table if exists $firsttable,${firsttable}_$id") || die "Got error on drop table: $dbh->errstr\n";
  141.   $dbh->disconnect; $dbh=0;
  142.   print "Test_drop: Did a drop $i times\n";
  143.   exit(0);
  144. }
  145.  
  146.  
  147. #
  148. # select records
  149. #
  150.  
  151. sub test_select
  152. {
  153.   my ($dbh,$i,$sth,@row,$error_counter,$sleep_time);
  154.  
  155.   $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
  156.               $opt_user, $opt_password,
  157.             { PrintError => 0}) || die $DBI::errstr;
  158.  
  159.   $error_counter=0;
  160.   $sleep_time=3;
  161.   for ($i=0 ; $i < $opt_loop_count ; $i++)
  162.   {
  163.     sleep($sleep_time);
  164.     $sth=$dbh->prepare("select sum(t.id) from $firsttable as t,$firsttable as t2") || die "Got error on select: $dbh->errstr;\n";
  165.     if ($sth->execute)
  166.     {
  167.       @row = $sth->fetchrow_array();
  168.       $sth->finish;
  169.       $sleep_time=3;
  170.     }
  171.     else
  172.     {
  173.       print "Warning; Got error from select: " . $dbh->errstr . "\n" if (! ($dbh->errstr =~ /doesn't exist/));
  174.       $sth->finish;
  175.       last if ($error_counter++ == 5);
  176.       $sleep_time=1;
  177.     }
  178.   }
  179.   $dbh->disconnect; $dbh=0;
  180.   print "Test_select: ok\n";
  181.   exit(0);
  182. }
  183.  
  184. #
  185. # flush records
  186. #
  187.  
  188. sub test_flush
  189. {
  190.   my ($dbh,$i,$sth,@row,$error_counter,$sleep_time);
  191.  
  192.   $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
  193.               $opt_user, $opt_password,
  194.             { PrintError => 0}) || die $DBI::errstr;
  195.  
  196.   $error_counter=0;
  197.   $sleep_time=5;
  198.   for ($i=0 ; $i < $opt_loop_count ; $i++)
  199.   {
  200.     sleep($sleep_time);
  201.     $sth=$dbh->prepare("select count(*) from $firsttable") || die "Got error on prepar: $dbh->errstr;\n";
  202.     if ($sth->execute)
  203.     {
  204.       @row = $sth->fetchrow_array();
  205.       $sth->finish;
  206.       $sleep_time=5;
  207.       $dbh->do("flush tables $firsttable") || die "Got error on flush table: " . $dbh->errstr . "\n";
  208.     }
  209.     else
  210.     {
  211.       print "Warning; Got error from select: " . $dbh->errstr . "\n" if (! ($dbh->errstr =~ /doesn't exist/));
  212.       $sth->finish;
  213.       last if ($error_counter++ == 5);
  214.       $sleep_time=1;
  215.     }
  216.   }
  217.   $dbh->disconnect; $dbh=0;
  218.   print "Test_select: ok\n";
  219.   exit(0);
  220. }
  221.